import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.Calendar;
import java.util.Date;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SpinnerDateModel;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SpringLayout;


public class EventDialog extends JDialog implements ActionListener{

	JPanel fields;
	JPanel buttons;
	
	private JTextField summaryField;
	private JTextField LocationField;
	private JSpinner StartDateSelector;
	private JSpinner StartHourSelector;
	private JSpinner EndDateSelector;
	private JSpinner EndHourSelector;
	private JComboBox priority;
	private JTextArea description; 
	private JButton ok;
	private JButton cancel;
	
	private Application mainController;
	
	JPanel createDateSelector (JSpinner date, JSpinner hour)
	{	 
	    String hours[] = new String [48];
		for (int h=0,j=0 ; h<24 ; h++,j+=2)
		{
			hours[j]=String.valueOf(h)+":00";
			hours[j+1]=String.valueOf(h)+":30";
		}
		
		JPanel DateSelector = new JPanel (new FlowLayout());
		SpinnerListModel hourModel = new SpinnerListModel(hours);
		SpinnerDateModel model = new SpinnerDateModel();
		Calendar d=Calendar.getInstance();
		date = new JSpinner(model);
        JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(date, "dd:MM:yy");
        date.setEditor(dateEditor);
        date.setValue(d.getTime());
        
        hour=new JSpinner (hourModel);
        JComponent editor= hour.getEditor();
        JFormattedTextField ftf=((JSpinner.DefaultEditor)editor).getTextField();
        ftf.setColumns(4);
        DateSelector.add(new JLabel("Data:"));
        DateSelector.add(date);
        DateSelector.add(new JLabel("Godzina:"));
        DateSelector.add(hour);
		 
		return DateSelector;
		
	}
	JComboBox createPrioritySelector (JComboBox c)
	{
		String values [] = {"Niski", "Normalny", "Wysoki"};
		c = new JComboBox (values);
		c.setSelectedIndex(1);
		return c;
	}
	void addAllComponents ()
	{
		fields = new JPanel();
		fields.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.gridwidth = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;                 
        fields.add(new JLabel("Podsumowanie"), c);
		 c.gridwidth = GridBagConstraints.REMAINDER;
         c.fill = GridBagConstraints.HORIZONTAL;
         c.weightx = 1.0;
         fields.add(summaryField = new JTextField(), c);
		
		c.gridwidth = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;                 
        fields.add(new JLabel("Położenie"), c);
		 c.gridwidth = GridBagConstraints.REMAINDER;
         c.fill = GridBagConstraints.HORIZONTAL;
         c.weightx = 1.0;
         fields.add(summaryField = new JTextField(), c);
		
		c.gridwidth = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;                 
        fields.add(new JLabel("Początek"), c);
		 c.gridwidth = GridBagConstraints.REMAINDER;
         c.fill = GridBagConstraints.HORIZONTAL;
         c.weightx = 1.0;
         fields.add(createDateSelector(StartDateSelector,StartHourSelector), c);
		
		c.gridwidth = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;     
        fields.add(new JLabel("Koniec"), c);
		 c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        fields.add(createDateSelector(EndDateSelector,EndHourSelector), c);
		
		c.gridwidth = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;     
        fields.add(new JLabel("Priorytet"), c);
		 c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        fields.add(createPrioritySelector(priority), c);
		
		c.gridwidth = GridBagConstraints.RELATIVE;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0.0;     
        fields.add(new JLabel("Opis"), c);
		c.gridwidth = GridBagConstraints.REMAINDER;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1.0;
        
        description = new JTextArea(4,4);
        description.setLineWrap(true);
        JScrollPane areaScrollPane = new JScrollPane(description);
        fields.add(areaScrollPane, c);
        
        buttons = new JPanel();
        buttons.setLayout(new FlowLayout());
        buttons.add(ok = new JButton("OK"));
        buttons.add(cancel = new JButton("Anuluj"));
		
		
		
	}
	
	
	class Events implements ActionListener 
	{
		public void actionPerformed(ActionEvent evt) 
		{
			if (evt.getSource()==ok)
			{
				applyAndClose();
			    return;
			}
			if(evt.getSource()==cancel)
			{
				closeWindow();
				return;
			}
		}

	}
	
	void applyAndClose ()
	{}
	void closeWindow()
	{
		processWindowEvent( new WindowEvent( this, WindowEvent.WINDOW_CLOSING) );
	}
	
	
	EventDialog (Frame owner, String title, Application c)
	{
		super(owner,title);
		this.mainController=c;
		addAllComponents();
		setLayout(new BorderLayout());
		add(fields,BorderLayout.NORTH);
		add(buttons,(BorderLayout.SOUTH));
		ActionListener l  = new Events();
		ok.addActionListener(l);
		cancel.addActionListener(l);
		setSize(500, 250);		
		
	}
	
	EventDialog (Frame owner, String title, Calendar date)
	{
		
	}

	
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
